﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;

namespace WebMVC.Helpers
{
    public class IPCheckAttribute : ActionFilterAttribute
    {
        private static string officeIP = System.Configuration.ConfigurationManager.AppSettings["officePCIP"];
        

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            bool isOfficePC = filterContext.HttpContext.Request.UserHostAddress == officeIP;

            if (!isOfficePC && !filterContext.HttpContext.User.IsInRole("superadmin"))
            {
                filterContext.Controller.TempData["error"] = "You don't have permission to access the requested page";
                filterContext.Result = new RedirectResult("/AdminPanel");
            }
        }

    }
}